﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace BLL.Dal
{

    /// <summary>
    /// 数据访问类:CommNewsDAL
    /// </summary>
    public partial class CommNewsDAL
    {
        public CommNewsDAL()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string NewsKey)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbNews");
            strSql.Append(" where NewsKey=@NewsKey ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@NewsKey", OleDbType.VarChar,255)			};
            parameters[0].Value = NewsKey;

            return DbHelperOleDb.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.CommNews model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbNews(");
            strSql.Append("NewsKey,Title,Content,NewSource,Author,KeyWord,CreateTime,Creator,UpdateTime,Updator,ImageUrl,NewsType)");
            strSql.Append(" values (");
            strSql.Append("@NewsKey,@Title,@Content,@NewSource,@Author,@KeyWord,@CreateTime,@Creator,@UpdateTime,@Updator,@ImageUrl,@NewsType)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@NewsKey", OleDbType.VarChar,255),
					new OleDbParameter("@Title", OleDbType.VarChar,255),
					new OleDbParameter("@Content", OleDbType.VarChar,255),
					new OleDbParameter("@NewSource", OleDbType.VarChar,255),
					new OleDbParameter("@Author", OleDbType.VarChar,255),
					new OleDbParameter("@KeyWord", OleDbType.VarChar,255),
					new OleDbParameter("@CreateTime", OleDbType.Date),
					new OleDbParameter("@Creator", OleDbType.VarChar,255),
					new OleDbParameter("@UpdateTime", OleDbType.Date),
					new OleDbParameter("@Updator", OleDbType.VarChar,255),
					new OleDbParameter("@ImageUrl", OleDbType.VarChar,255),
					new OleDbParameter("@NewsType", OleDbType.VarChar,255)};
            parameters[0].Value = model.NewsKey;
            parameters[1].Value = model.Title;
            parameters[2].Value = model.Content;
            parameters[3].Value = model.NewSource;
            parameters[4].Value = model.Author;
            parameters[5].Value = model.KeyWord;
            parameters[6].Value = model.CreateTime;
            parameters[7].Value = model.Creator;
            parameters[8].Value = model.UpdateTime;
            parameters[9].Value = model.Updator;
            parameters[10].Value = model.ImageUrl;
            parameters[11].Value = model.NewsType;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.CommNews model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbNews set ");
            strSql.Append("Title=@Title,");
            strSql.Append("Content=@Content,");
            strSql.Append("NewSource=@NewSource,");
            strSql.Append("Author=@Author,");
            strSql.Append("KeyWord=@KeyWord,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("Creator=@Creator,");
            strSql.Append("UpdateTime=@UpdateTime,");
            strSql.Append("Updator=@Updator,");
            strSql.Append("ImageUrl=@ImageUrl,");
            strSql.Append("NewsType=@NewsType");
            strSql.Append(" where NewsKey=@NewsKey ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@Title", OleDbType.VarChar,255),
					new OleDbParameter("@Content", OleDbType.VarChar,255),
					new OleDbParameter("@NewSource", OleDbType.VarChar,255),
					new OleDbParameter("@Author", OleDbType.VarChar,255),
					new OleDbParameter("@KeyWord", OleDbType.VarChar,255),
					new OleDbParameter("@CreateTime", OleDbType.Date),
					new OleDbParameter("@Creator", OleDbType.VarChar,255),
					new OleDbParameter("@UpdateTime", OleDbType.Date),
					new OleDbParameter("@Updator", OleDbType.VarChar,255),
					new OleDbParameter("@ImageUrl", OleDbType.VarChar,255),
					new OleDbParameter("@NewsType", OleDbType.VarChar,255),
					new OleDbParameter("@NewsKey", OleDbType.VarChar,255)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.Content;
            parameters[2].Value = model.NewSource;
            parameters[3].Value = model.Author;
            parameters[4].Value = model.KeyWord;
            parameters[5].Value = model.CreateTime;
            parameters[6].Value = model.Creator;
            parameters[7].Value = model.UpdateTime;
            parameters[8].Value = model.Updator;
            parameters[9].Value = model.ImageUrl;
            parameters[10].Value = model.NewsType;
            parameters[11].Value = model.NewsKey;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string NewsKey)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbNews ");
            strSql.Append(" where NewsKey=@NewsKey ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@NewsKey", OleDbType.VarChar,255)			};
            parameters[0].Value = NewsKey;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string NewsKeylist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbNews ");
            strSql.Append(" where NewsKey in (" + NewsKeylist + ")  ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.CommNews GetModel(string NewsKey)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NewsKey,Title,Content,NewSource,Author,KeyWord,CreateTime,Creator,UpdateTime,Updator,ImageUrl,NewsType from tbNews ");
            strSql.Append(" where NewsKey=@NewsKey ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@NewsKey", OleDbType.VarChar,255)			};
            parameters[0].Value = NewsKey;

            Model.CommNews model = new Model.CommNews();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.CommNews DataRowToModel(DataRow row)
        {
            Model.CommNews model = new Model.CommNews();
            if (row != null)
            {
                if (row["NewsKey"] != null)
                {
                    model.NewsKey = row["NewsKey"].ToString();
                }
                if (row["Title"] != null)
                {
                    model.Title = row["Title"].ToString();
                }
                if (row["Content"] != null)
                {
                    model.Content = row["Content"].ToString();
                }
                if (row["NewSource"] != null)
                {
                    model.NewSource = row["NewSource"].ToString();
                }
                if (row["Author"] != null)
                {
                    model.Author = row["Author"].ToString();
                }
                if (row["KeyWord"] != null)
                {
                    model.KeyWord = row["KeyWord"].ToString();
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
                if (row["Creator"] != null)
                {
                    model.Creator = row["Creator"].ToString();
                }
                if (row["UpdateTime"] != null && row["UpdateTime"].ToString() != "")
                {
                    model.UpdateTime = DateTime.Parse(row["UpdateTime"].ToString());
                }
                if (row["Updator"] != null)
                {
                    model.Updator = row["Updator"].ToString();
                }
                if (row["ImageUrl"] != null)
                {
                    model.ImageUrl = row["ImageUrl"].ToString();
                }
                if (row["NewsType"] != null)
                {
                    model.NewsType = row["NewsType"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NewsKey,Title,Content,NewSource,Author,KeyWord,CreateTime,Creator,UpdateTime,Updator,ImageUrl,NewsType ");
            strSql.Append(" FROM tbNews ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbNews ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperOleDb.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {

            int pageSize = endIndex - startIndex;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from ( ");


            strSql.Append(" select top ");
            ///页大小
            strSql.Append(pageSize);
            strSql.Append(" * from ( ");

            #region
            strSql.Append(" select top ");
            strSql.Append(endIndex);
            strSql.Append(" * from tbNews ");
            strSql.Append(strWhere);
            strSql.Append("order by createTime desc");
            #endregion

            strSql.Append(")");
            strSql.Append("order by createTime asc");

            strSql.Append(")");
            strSql.Append("order by createTime desc");

            return DbHelperOleDb.Query(strSql.ToString());

        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        /// <param name="newsType">新闻类别</param>
        /// <param name="startIndex">开始索引</param>
        /// <param name="endIndex">结束索引</param>
        /// <returns></returns>
        public DataSet GetListByPage(string newsType, int startIndex, int endIndex)
        {

            int pageSize = endIndex - startIndex;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NewsKey,Title,NewSource,Author,KeyWord,CreateTime,Creator,UpdateTime,Updator,ImageUrl,NewsType,'' as temp_createtime  from ( ");
            strSql.Append(" select top ");
            ///页大小
            strSql.Append(pageSize);
            strSql.Append(" * from ( ");

            #region
            strSql.Append(" select top ");
            strSql.Append(endIndex);
            strSql.Append(" * from tbNews ");
            strSql.Append("where NewsType=@NewsType ");

            strSql.Append(" order by createTime desc");
            #endregion

            strSql.Append(")");
            strSql.Append(" order by createTime asc");

            strSql.Append(")");
            strSql.Append(" order by createTime desc");

            OleDbParameter[] parameters = {
					new OleDbParameter("@NewsType", OleDbType.VarChar,255)			};
            parameters[0].Value = newsType;

            return DbHelperOleDb.Query(strSql.ToString(), parameters);

        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OleDbParameter[] parameters = {
                    new OleDbParameter("@tblName", OleDbType.VarChar, 255),
                    new OleDbParameter("@fldName", OleDbType.VarChar, 255),
                    new OleDbParameter("@PageSize", OleDbType.Integer),
                    new OleDbParameter("@PageIndex", OleDbType.Integer),
                    new OleDbParameter("@IsReCount", OleDbType.Boolean),
                    new OleDbParameter("@OrderType", OleDbType.Boolean),
                    new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbNews";
            parameters[1].Value = "NewsKey";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
